<template>
	<view class="wrap">
		<view class="u-tabs-box">
			<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change"
				:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
		</view>

		<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
			<swiper-item class="swiper-item" v-for="(tab, indexT) in orderList" :key="indexT">
				<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="loadMore">
					<view class="page-box" v-if="orderList[swiperCurrent].length > 0">
						<view class="order" v-for="(order, indexL) in orderList[swiperCurrent]" :key="indexL"
							@click="$u.route(`/pages/pay/payment/payment?id=${order.order_id}`)">
							<view class="top">
								<view class="left">订单编号：{{ order.order_sn }}</view>
								<view class="ord-status-txt-ts fr" v-if="order.order_status == 1">待支付</view>

								<view class="ord-status-txt-ts fr" v-if="order.order_status == 2">待发货</view>

								<view class="ord-status-txt-ts fr" v-if="order.order_status == 3">待收货</view>

								<view class="ord-status-txt-ts fr" v-if="order.order_status == 4">待评价</view>

								<view class="ord-status-txt-ts fr" v-if="order.order_status == 5">已完成</view>

								<view class="ord-status-txt-ts fr" v-if="order.order_status == 6">退货中</view>

								<view class="ord-status-txt-ts fr" v-if="order.order_status == 7">订单关闭</view>

								<view class="ord-status-txt-ts fr" v-if="order.order_status == 8">拼团中</view>

								<view class="ord-status-txt-ts fr" v-if="order.order_status == 9">砍价中</view>

								<view class="ord-status-txt-ts fr" v-if="order.order_status == 10">拼团失败</view>

								<view class="ord-status-txt-ts fr" v-if="order.order_status == 11">砍价失败</view>
							</view>
							<scroll-view class="goods" scroll-y>
								<view class="item">
									<view class="left">
										<image :src="order.original_img" mode="aspectFill"></image>
									</view>
									<view class="right">
										<view class="content">
											<view class="title">{{ order.goods_name }}</view>
										</view>
										<view class="price">￥{{ order.goods_price }}</view>
										<view class="number">x{{ order.goods_num }}</view>
									</view>
								</view>
							</scroll-view>
							<view class="total">
								共{{ order.goods_num }}件商品 合计:
								<text class="total-price">￥{{ order.order_amount }}</text>
								<text>(含运费￥<b v-html="order.shipping_price"></b>)</text>
							</view>
							<view class="bottom">
								<view class="btn" @click.stop="$makePhoneCall()">联系客服</view>
								<view class="pay btn" v-if="order.order_status == 1" @click.stop="pay(order.order_id)">去支付
								</view>
								<view class="btn" v-if="list[swiperCurrent].name == '待收货'">查看物流</view>
								<view class="pay btn" v-if="order.order_status == 3">确认收货</view>
							</view>
						</view>
						<u-loadmore :status="loadStatus[swiperCurrent]" margin-top="50" margin-bottom="50"
							bgColor="#f8f8f8"></u-loadmore>
					</view>
					<view class="page-box" v-else>
						<view class="centre">
							<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode=""></image>
							<view class="explain">
								您还没有相关的订单
								<view class="tips">可以去看看有那些想买的</view>
							</view>
							<view class="btn" @click="goShop">随便逛逛</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderList: [
					[],
					[],
					[],
					[],
					[],
					[]
				],
				list: [{
						name: '全部',
						page: 1,
						status: 'order_status',
						code: 0
					}, // pay_status 支付状态(0:未支付;1:已支付;10:等待支付)
					{
						name: '待付款',
						page: 1,
						status: 'order_status',
						code: '0, 10'
					}, // shipping_status 发货状态(0:未发货;1:已发货;2:已收货;10:待发货)
					{
						name: '待分享',
						page: 1,
						status: 'order_status',
						code: 1
					},
					{
						name: '待发货',
						page: 1,
						status: 'order_status',
						code: 0
					}, // order_status 订单状态(0:已完成;1:未完成;2:已取消;)
					{
						name: '待收货',
						page: 1,
						status: 'order_status',
						code: 0
					},
					{
						name: '待评价',
						page: 1,
						status: 'order_status',
						code: 0
					}
				],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore']
			};
		},
		onLoad() {
			this.getOrderList();
		},
		methods: {
			loadMore() {
				if (this.loadStatus[this.swiperCurrent] === 'loadmore') {
					this.getOrderList();
				}
			},
			getOrderList() {
				let index = this.swiperCurrent;
                console.log(index)
				this.loadStatus[index] = 'loading';
				//portal/order/my_all_orders
				this.$api.get({
					url: 'portal/order/get_order_list',
					data: {
						...this.list[index],
						field: 'id,order_sn,total_amount',
						size: 10 ,// 每页显示几条
						tab: index
					},
					success: res => {
						console.log(res.data)
						if (res.code === 1) {
							let orderList = res.data;
							// 分页判断
							if (orderList.length <= 10) {
								orderList.forEach(item => {
									this.orderList[index].push(item);
								});
								this.loadStatus[index] = 'nomore';
							} else {
								this.loadStatus[index] = 'loadmore';
								this.list[index].page++;
							}
						} else {
							this.$toast(res.msg || '数据获取失败');
						}
					}
				});
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
				if (this.orderList[this.swiperCurrent].length === 0) {
					this.loadMore();
				}
			},
			// 切换动画
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			// 切换动画
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
				if (this.orderList[this.swiperCurrent].length === 0) {
					this.loadMore();
				}
			},
			goShop() {
				uni.switchTab({
					url: '../shop/shop'
				});
			},
			pay(id) {
				this.$u.route('/pages/pay/payment/payment?id=' + id);
			}
		}
	};
</script>
<style lang="scss" scoped>
	.ord-status-txt-ts {
		font-size: 13px;
		color: #ee7800;
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.goods {
			max-height: 520rpx;
		}

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				text-align: right;
				flex: 1;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: flex-end;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
				margin-left: 30rpx;
			}

			.pay {
				background: rgb(242, 145, 0);
				color: #fff;
			}

			.del {
				background: #f40;
				color: #fff;
			}
		}
	}

	.swiper-box {
		flex: 1;

		.swiper-item {
			height: 100%;
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}
</style>
